package foo.bar.blog.web.action.general;

import foo.bar.blog.web.action.BaseActionBean;
import foo.bar.blog.web.extension.BlogTypeConverter;
import foo.bar.blog.web.extension.EntryTypeConverter;
import foo.bar.blog.core.services.BlogService;
import foo.bar.blog.core.domain.Blog;
import foo.bar.blog.core.domain.Resource;
import foo.bar.blog.core.domain.Entry;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;

import java.io.ByteArrayInputStream;

/**
 * @author tmjee
 * @version $Date$ $Id$
 */
public class ResourceRetrievalActionBean extends BaseActionBean {

    public static final String VIEW = "";

    private BlogService blogService;

    @Validate(trim = true, required=true)
    private String resourceName;

    @Validate(trim=true, required=true, converter = EntryTypeConverter.class)
    private Entry entry;

    public Resolution view() {
        Resource resource = blogService.getResourceByNameAndEntryId(resourceName, entry.getId());
        return new StreamingResolution(resource.getMimeType(), new ByteArrayInputStream(resource.getContent()));
    }

    public String getResourceName() {
        return resourceName;
    }

    public void setResourceName(String resourceName) {
        this.resourceName = resourceName;
    }

    public Entry getEntry() {
        return entry;
    }

    public void setEntry(Entry entry) {
        this.entry = entry;
    }

    @SpringBean("blogService")
    public void setBlogService(BlogService blogService) {
        this.blogService = blogService;
    }
}



